// 初始化Three.js场景
let scene, camera, renderer;
let stars = [];
let nebulae = [];
let mouseX = 0, mouseY = 0;
let windowHalfX = window.innerWidth / 2;
let windowHalfY = window.innerHeight / 2;

// 颜色定义
const colors = {
    purple: 0x8B5CF6,
    lightPurple: 0xC084FC,
    pink: 0xF472B6
};

init();
animate();

// 初始化场景
function init() {
    // 创建场景
    scene = new THREE.Scene();
    
    // 创建相机
    camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 2000);
    camera.position.z = 50;
    
    // 创建渲染器
    renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
    renderer.setSize(window.innerWidth, window.innerHeight);
    renderer.setPixelRatio(window.devicePixelRatio);
    
    // 将渲染器添加到页面
    document.getElementById('background-container').appendChild(renderer.domElement);
    
    // 创建星空
    createStars();
    
    // 创建星云
    createNebulae();
    
    // 添加事件监听器
    document.addEventListener('mousemove', onDocumentMouseMove);
    window.addEventListener('resize', onWindowResize);
    
    // 添加点击涟漪效果
    document.addEventListener('click', createRipple);
    
    // 添加发帖按钮事件
    document.getElementById('new-post-btn').addEventListener('click', openEditor);
    document.getElementById('close-editor').addEventListener('click', closeEditor);
    
    // 添加悬停效果到内容卡片
    const cards = document.querySelectorAll('.content-card');
    cards.forEach(card => {
        card.addEventListener('mouseenter', function() {
            this.style.boxShadow = `0 0 20px ${colors.lightPurple}`;
        });
        
        card.addEventListener('mouseleave', function() {
            this.style.boxShadow = '0 10px 25px rgba(139, 92, 246, 0.3)';
        });
    });
}

// 创建星空
function createStars() {
    const starGeometry = new THREE.BufferGeometry();
    const starMaterial = new THREE.PointsMaterial({
        color: 0xFFFFFF,
        size: 1.5,
        sizeAttenuation: true
    });
    
    const starVertices = [];
    for (let i = 0; i < 5000; i++) {
        const x = THREE.MathUtils.randFloatSpread(2000);
        const y = THREE.MathUtils.randFloatSpread(2000);
        const z = THREE.MathUtils.randFloatSpread(2000);
        starVertices.push(x, y, z);
    }
    
    starGeometry.setAttribute('position', new THREE.Float32BufferAttribute(starVertices, 3));
    const starField = new THREE.Points(starGeometry, starMaterial);
    scene.add(starField);
    stars.push(starField);
}

// 创建星云
function createNebulae() {
    // 创建2-3个星云
    const nebulaCount = 2 + Math.floor(Math.random() * 2);
    
    for (let i = 0; i < nebulaCount; i++) {
        // 创建星云几何体
        const geometry = new THREE.SphereGeometry(
            20 + Math.random() * 30, 
            32, 
            32
        );
        
        // 创建星云材质
        const material = new THREE.MeshBasicMaterial({
            color: i % 2 === 0 ? colors.purple : colors.pink,
            transparent: true,
            opacity: 0.2,
            blending: THREE.AdditiveBlending
        });
        
        // 创建星云网格
        const nebula = new THREE.Mesh(geometry, material);
        
        // 随机定位
        nebula.position.x = -100 + Math.random() * 200;
        nebula.position.y = -100 + Math.random() * 200;
        nebula.position.z = -200 + Math.random() * 200;
        
        // 添加到场景
        scene.add(nebula);
        nebulae.push(nebula);
    }
}

// 鼠标移动事件
function onDocumentMouseMove(event) {
    mouseX = (event.clientX - windowHalfX) / 100;
    mouseY = (event.clientY - windowHalfY) / 100;
}

// 窗口大小调整事件
function onWindowResize() {
    windowHalfX = window.innerWidth / 2;
    windowHalfY = window.innerHeight / 2;
    
    camera.aspect = window.innerWidth / window.innerHeight;
    camera.updateProjectionMatrix();
    
    renderer.setSize(window.innerWidth, window.innerHeight);
}

// 动画循环
function animate() {
    requestAnimationFrame(animate);
    
    // 缓慢旋转星云
    nebulae.forEach(nebula => {
        nebula.rotation.y += 0.001;
        nebula.rotation.x += 0.0005;
    });
    
    // 相机跟随鼠标轻微移动
    camera.position.x += (mouseX - camera.position.x) * 0.05;
    camera.position.y += (-mouseY - camera.position.y) * 0.05;
    camera.lookAt(scene.position);
    
    // 渲染场景
    renderer.render(scene, camera);
}

// 创建涟漪效果
function createRipple(event) {
    const ripple = document.createElement('div');
    ripple.className = 'ripple';
    ripple.style.left = `${event.clientX}px`;
    ripple.style.top = `${event.clientY}px`;
    document.body.appendChild(ripple);
    
    // 动画
    let size = 0;
    const maxSize = 100;
    const animateRipple = () => {
        size += 2;
        ripple.style.width = `${size}px`;
        ripple.style.height = `${size}px`;
        ripple.style.opacity = (1 - size / maxSize).toString();
        
        if (size < maxSize) {
            requestAnimationFrame(animateRipple);
        } else {
            document.body.removeChild(ripple);
        }
    };
    
    animateRipple();
}

// 打开发帖编辑器
function openEditor() {
    document.getElementById('post-editor').classList.add('active');
}

// 关闭发帖编辑器
function closeEditor() {
    document.getElementById('post-editor').classList.remove('active');
}